import Vue from 'vue'
import VueRouter from 'vue-router'
import progress from "nprogress"
import 'nprogress/nprogress.css'

import {Toast} from "vant";
//组件
import Login from "../views/Login.vue";
import Register from "../views/Register";

import Home from "@/views/Home.vue";
import Index from "@/views/Home/Index.vue";
import CartIndex from "@/views/Home/CartIndex";
import UserIndex from "@/views/Home/UserIndex";
import GoodsDetail from "@/views/Goods/GoodsDetail";
import HotSell from "../views/More/hotSell";
import SaleSell from "../views/More/saleSell";
import Search from "../views/Goods/Sreach.vue";
import SubmitOrders from "../views/Goods/submitOrders";


import AddressList from "../views/address/addressList";
import addressEdit from "../views/address/addressEdit";
import OrderList from "../views/order/OrderList";
import ResetPwd from "../views/safe/ChangeLoginPwd";
import SafeIndex from "../views/safe/index.vue";
import updateUserInfo from "../views/safe/updateUserInfo";
import ChangeNickname from "../views/safe/ChangeNickname";
import Safe from "../views/safe/Safe";
import ProblemFeedback from "../views/safe/ProblemFeedback";
import about from "../views/about";
import ChangePayPassword from "../views/safe/changePayPassword";

import AllOrderList from "../views/order/AllOrderList";
import OrderDetail from "../views/order/OrderDetail";
import LogisticsInformation from "@/views/order/LogisticsInformation";
import RefundView from "@/views/order/RefundView.vue";
import Payment from "@/views/safe/Payment";

Vue.use(VueRouter);

const routes = [
    {path: "/login", name: "Login", component: Login},
    {path: "/register", name: "Register", component: Register},
    {
        path: "/", redirect: "index", component: Home,
        children: [
            {path: "index", name: "index", component: Index},
            {path: "cartIndex", name: "CartIndex", component: CartIndex},
            {path: "userIndex", name: "UserIndex", component: UserIndex},
        ]
    },
    {path: "/goodsDetail", name: "goodsDetail", component: GoodsDetail},
    {path: "/search", name: "search", component: Search},
    {path: "/more/hotSell", name: "hotSell", component: HotSell},
    {path: "/more/saleSell", name: "saleSell", component: SaleSell},
    {path: "/submitOrders", name: "submitOrders", component: SubmitOrders},
    {path: "/address/list", name: "addressList", component: AddressList},
    {path: "/address/edit", name: "addressEdit", component: addressEdit},

    {
        path: "/safe", redirect: "/", component: Safe,
        children: [
            {path: "/", name: "safeIndex", component: SafeIndex},
            {path: "resetPwd", name: "resetPwd", component: ResetPwd},
            {path: "updateUserInfo", name: "updateUserInfo", component: updateUserInfo},
            {path: "changeNick", name: "changeNick", component: ChangeNickname},
            {path: "feedback", name: "feedback", component: ProblemFeedback},
        ]
    },

    {path: "/safe/payment", name: "payment", component: Payment},
    {path: "/safe/changePayPassword", name: "ChangePayPassword", component: ChangePayPassword},

    {path: "/order", name: "orderList", component: OrderList},
    {path: "/allOrder", name: "allOrder", component: AllOrderList},
    {path: "/orderDetail", name: "orderDetail", component: OrderDetail},
    {path: "/logisticsInformation", name: "LogisticsInformation", component: LogisticsInformation},
    {path: "/refund", name: "Refund", component: RefundView},

    {path: "/about", name: "about", component: about},


];


const router = new VueRouter({
    mode: 'history',
    base: process.env.BASE_URL,
    routes
});

//路由守卫
router.beforeEach((to, from, next) => {
    progress.start(); // 开启进度条
    if (to.path === "/login" || to.path === "/register" || to.path === "/" || to.path === "/index" || to.path === "/userIndex" || to.path === "/goodsDetail"
        || to.path === "/More/hotSell" || to.path === "/More/saleSell" || to.path === "/search" || to.path === "/about"
    ) {
        next();
    } else {
        // 进行一次判断是否登录了
        if (!localStorage.getItem("eleToken")) {
            progress.done();
            Toast("请先登录");
        } else {
            next();
        }
    }
});


router.afterEach(() => {
    progress.done()
    // 关闭进度条
});


export default router
